package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.CarCharge;
import com.group.Enity.Complaint;
import com.group.Service.ComplaintService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("complaint")
public class ComplaintController {

    @Autowired
    private ComplaintService complaintService;


    @ResponseBody
    @RequestMapping("queryComplaintAll")
    public String queryComplaintAll(Integer page,Integer limit,Integer status,Integer comId){
        PageHelper.startPage(page,limit);
        List<Complaint> list = complaintService.getComplaints(status,comId);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("queryComplaintAll2")
    public String queryComplaintAll2(Integer page,Integer limit,Integer status,Integer comId){
        PageHelper.startPage(page,limit);
        List<Complaint> list = complaintService.getComplaints(status,comId);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("add")
    public String add(@RequestBody JSONObject jsonObject)
    {

        //System.out.println(jsonObject);
        String comId = jsonObject.getStr("comId");
        String remarks = jsonObject.getStr("remarks");

        complaintService.addComplaint(Integer.parseInt(comId),remarks);

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        json.accumulate("msg","hello world");
        return json.toString();

    }

    @ResponseBody
    @PostMapping("deleteByIds")
    public String deleteByIds(String ids)
    {

        String[] list = ids.split(",");
        for (int step = 0;step<list.length;step++)
        {
            int id = Integer.parseInt(list[step]);
            complaintService.delById(id);
        }
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();

    }

    @ResponseBody
    @RequestMapping("update")
    public JSONObject update(Integer id)
    {
        //System.out.println(id);
        complaintService.updateStatus(id);
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }



}
